package org.dydl.controller.system;

import org.dydl.common.dto.ResponseMessage;
import org.dydl.model.system.Goods;
import org.dydl.model.system.Product;
import org.dydl.service.system.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 原料、成品接口
 * @author 张华城
 *
 */
@RestController
@RequestMapping("/api/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;


    /**
     * 保存或修改 商品信息
     * @param goods 商品
     * @return
     */
    @RequestMapping("/save")
    public ResponseMessage save(@RequestBody Product product) {
            ResponseMessage res = goodsService.save(product);
            return res;
    }
    /**
     * 查询商品
     */
    @RequestMapping("/list")
    public ResponseMessage getGoods(int page, int rows, String name ) {
        List<Sort.Order> orders=new ArrayList<Sort.Order>();
        orders.add( new Sort.Order(Sort.Direction. DESC, "createDate"));
        Pageable pageable = new PageRequest(page - 1, rows,new Sort(orders));
        ResponseMessage goodsList = goodsService.getGoodLists(name,pageable);
        return goodsList;
    }

    /**
     * 删除商品
     */
    @RequestMapping("/delete")
    public boolean delete(int[] ids) {
        if (ids.length != 0) {
            for (int i = 0; i < ids.length; i++) {
                goodsService.delete(ids[i]);
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据sku模糊查询成品商品信息
     * @param sku
     * @return
     */
    @RequestMapping("/getGoodsBySku")
    public ResponseMessage getGoodsBySku(String sku){return  goodsService.getGoodsBySku(sku,"CP");}

    /**
     * 根据名称模糊查询原料或者辅料信息
     * @param name
     * @return
     */
    @RequestMapping("/getMaterial")
    public ResponseMessage getMaterialByName(String name){return  goodsService.getMaterialByName(name);}

    /**
     * 根据uuid查询goods列表
     * @param uuid
     * @return
     */
    @RequestMapping("/findByName")
    public List<Product> findByLossUuid(@RequestParam(value = "name", required = false) String name) {

        return goodsService.findByName(name);

    }
    
    /**
     * 自动匹配商品
     */
    @RequestMapping("/get")
    public List<Goods> get(@RequestParam(value = "sku", required = false) String sku) {
    	List<Goods> res = null;
        if (!sku.isEmpty()) {
             res = goodsService.get(sku);
        }
        return res;
    }


}
